<template>
  <div class="aging-test-trend">
    <el-card class="trend-card">
      <template #header>
        <div class="card-header">
          <h2>老化趋势图查看页</h2>
        </div>
      </template>
      
      <!-- 图表类型选择 -->
      <div class="chart-type-selector">
        <div class="chart-type-item active">
          <span>折线图:</span>
          <span>展示老化时间趋势，产品光衰 vs 时间</span>
        </div>
      </div>

      <!-- 老化趋势分析图表 -->
      <div class="trend-chart-section">
        <div class="chart-header">
          <h3>老化趋势分析</h3>
          <div class="chart-controls">
            <el-button size="small" icon="Download" @click="downloadAgingTrendImage">下载图片</el-button>
          </div>
        </div>
        <div class="chart-container">
          <div ref="agingTrendChart" style="width: 100%; height: 300px;"></div>
        </div>
      </div>

      <!-- 产品光衰 vs 时间图表 -->
      <div class="trend-chart-section">
        <div class="chart-header">
          <h3>产品光衰 vs 时间</h3>
          <div class="chart-controls">
            <el-button size="small" icon="Download" @click="downloadLightDecayImage">下载图片</el-button>
          </div>
        </div>
        <div class="chart-container">
          <div ref="lightDecayChart" style="width: 100%; height: 300px;"></div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <el-button type="primary" @click="downloadAllImages">下载所有图片</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, nextTick } from 'vue'
import * as echarts from 'echarts'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const batchId = ref(route.query.id as string)

// 图表控制
const showComparison = ref(false)
const showLightComparison = ref(false)

// 图表相关
const agingTrendChart = ref()
const lightDecayChart = ref()
let agingTrendChartInstance: echarts.ECharts | null = null
let lightDecayChartInstance: echarts.ECharts | null = null

// 老化趋势数据
const agingTrendData = ref({
  dates: ['12-01', '12-02', '12-03', '12-04', '12-05', '12-06', '12-07', '12-08', '12-09'],
  values: [4.5, 4.2, 4.3, 3.8, 4.0, 3.5, 3.8, 3.2, 2.8]
})

// 光衰数据
const lightDecayData = ref({
  hours: [1000, 3000, 5000, 7000, 10000],
  values: [100, 100, 100, 95, 85]
})

// 下载老化趋势图片
const downloadAgingTrendImage = () => {
  if (agingTrendChartInstance) {
    const url = agingTrendChartInstance.getDataURL({
      type: 'png',
      pixelRatio: 2,
      backgroundColor: '#fff'
    })
    const link = document.createElement('a')
    link.href = url
    link.download = '老化趋势分析图.png'
    link.click()
  }
}

// 下载光衰图片
const downloadLightDecayImage = () => {
  if (lightDecayChartInstance) {
    const url = lightDecayChartInstance.getDataURL({
      type: 'png',
      pixelRatio: 2,
      backgroundColor: '#fff'
    })
    const link = document.createElement('a')
    link.href = url
    link.download = '产品光衰vs时间图.png'
    link.click()
  }
}

// 下载所有图片
const downloadAllImages = () => {
  downloadAgingTrendImage()
  setTimeout(() => {
    downloadLightDecayImage()
  }, 500) // 延迟500ms避免同时下载
}

// 初始化老化趋势图
const initAgingTrendChart = () => {
  if (!agingTrendChart.value) return
  
  agingTrendChartInstance = echarts.init(agingTrendChart.value)
  
  const option = {
    title: {
      text: '老化趋势分析',
      left: 'center',
      textStyle: {
        fontSize: 16,
        fontWeight: 'bold'
      }
    },
    tooltip: {
      trigger: 'axis',
      formatter: (params: any) => {
        const param = params[0]
        return `${param.axisValue}<br/>老化指数: ${param.value}`
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: agingTrendData.value.dates,
      axisLabel: {
        fontSize: 12
      }
    },
    yAxis: {
      type: 'value',
      name: '老化指数',
      axisLabel: {
        fontSize: 12
      }
    },
    series: [{
      name: '老化指数',
      type: 'line',
      data: agingTrendData.value.values,
      smooth: true,
      lineStyle: {
        color: '#FF9900',
        width: 2
      },
      itemStyle: {
        color: '#FF9900'
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
            offset: 0, color: 'rgba(255, 153, 0, 0.3)'
          }, {
            offset: 1, color: 'rgba(255, 153, 0, 0.1)'
          }]
        }
      }
    }]
  }
  
  agingTrendChartInstance.setOption(option)
  
  // 响应式调整
  window.addEventListener('resize', () => {
    agingTrendChartInstance?.resize()
  })
}

// 初始化光衰图表
const initLightDecayChart = () => {
  if (!lightDecayChart.value) return
  
  lightDecayChartInstance = echarts.init(lightDecayChart.value)
  
  const option = {
    title: {
      text: '产品光衰 vs 时间',
      left: 'center',
      textStyle: {
        fontSize: 16,
        fontWeight: 'bold'
      }
    },
    tooltip: {
      trigger: 'axis',
      formatter: (params: any) => {
        const param = params[0]
        return `${param.axisValue}小时<br/>光衰率: ${param.value}%`
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: lightDecayData.value.hours,
      name: '时间(小时)',
      axisLabel: {
        fontSize: 12
      }
    },
    yAxis: {
      type: 'value',
      name: '光衰率(%)',
      min: 80,
      max: 100,
      axisLabel: {
        formatter: '{value}%',
        fontSize: 12
      }
    },
    series: [{
      name: '光衰率',
      type: 'line',
      data: lightDecayData.value.values,
      smooth: true,
      lineStyle: {
        color: '#FF9900',
        width: 2
      },
      itemStyle: {
        color: '#FF9900'
      },
      markLine: {
        data: [{
          yAxis: 90,
          lineStyle: {
            color: '#E6A23C',
            type: 'dashed'
          },
          label: {
            formatter: '警戒线: 90%'
          }
        }]
      }
    }]
  }
  
  lightDecayChartInstance.setOption(option)
  
  // 响应式调整
  window.addEventListener('resize', () => {
    lightDecayChartInstance?.resize()
  })
}

// 页面加载时获取数据
onMounted(() => {
  console.log('加载批次ID:', batchId.value)
  // 这里应该根据batchId获取详细数据并生成图表
  nextTick(() => {
    initAgingTrendChart()
    initLightDecayChart()
  })
})
</script>

<style lang="scss" scoped>
.aging-test-trend {
  padding: 0;
  background-color: #f5f5f5;

  .trend-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      h2 {
        margin: 0;
        font-size: 18px;
        font-weight: bold;
      }
    }

    .chart-type-selector {
      display: flex;
      border-bottom: 1px solid #ebeef5;
      margin-bottom: 20px;

      .chart-type-item {
        padding: 10px 20px;
        cursor: pointer;
        border-bottom: 2px solid transparent;

        &.active {
          border-bottom: 2px solid #409eff;
          color: #409eff;
        }
      }
    }

    .trend-chart-section {
      margin-bottom: 30px;

      .chart-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        h3 {
          margin: 0;
          font-size: 16px;
        }

        .chart-controls {
          display: flex;
          align-items: center;
          gap: 15px;
        }
      }

      .chart-container {
        .chart-legend {
          display: flex;
          justify-content: center;
          margin-top: 10px;

          .legend-item {
            display: flex;
            align-items: center;
            margin-right: 20px;

            .legend-color {
              display: inline-block;
              width: 12px;
              height: 12px;
              margin-right: 5px;
              border-radius: 2px;
            }
          }
        }
      }
    }

    .action-buttons {
      display: flex;
      justify-content: center;
      gap: 20px;
      margin-top: 30px;
    }
  }
}
</style>